﻿namespace Journey.Web.Areas.Users.Controllers
{
    using Journey.Application.Users;
    using Journey.DTO.Users;
    using Journey.Settings;
    using System.Web.Mvc;

    /// <summary>
    /// 用户控制器。
    /// </summary>
    public class UserController : Controller
    {
        /// <summary>
        /// 站点信息。
        /// </summary>
        private readonly ISite _site;

        /// <summary>
        /// 用户应用服务。
        /// </summary>
        private readonly IUserAppService _userAppService;

        /// <summary>
        /// 初始化用户控制器。
        /// </summary>
        /// <param name="userAppService">用户应用服务。</param>
        public UserController(IUserAppService userAppService)
        {
            this._userAppService = userAppService;

            this._site = SiteFactory.Current;
        }

        #region 用户首页

        /// <summary>
        /// 用户首页。
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

        #endregion 用户首页

        #region 用户信息查看

        /// <summary>
        /// 用户信息查看。
        /// </summary>
        /// <returns></returns>
        public ActionResult Show(string accountNumber)
        {
            var user = this._userAppService.GetUser(accountNumber);

            if (user != null)
            {
                return View(user);
            }

            return this.Redirect(this._site.Error404Page);
        }

        #endregion 用户信息查看

        #region 用户注册

        /// <summary>
        /// 用户注册。
        /// </summary>
        /// <returns></returns>
        public ActionResult Register()
        {
            if (this.Request.IsAuthenticated)
            {
                return this.Redirect(this._site.HomePage);
            }

            return View();
        }

        /// <summary>
        /// 用户注册。
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Register(UserDTO userDTO)
        {
            if (this.ModelState.IsValid)
            {
                var status = this._userAppService.RegisterNewUser(userDTO);

                if (RegisterNewUserStatus.Success == status)
                {
                    return View("Success");
                }
            }

            return View(userDTO);
        }

        #endregion 用户注册

        #region 释放资源

        /// <summary>
        /// 释放资源。
        /// </summary>
        /// <param name="disposing"></param>
        protected override void Dispose(bool disposing)
        {
            this._userAppService.Dispose();

            base.Dispose(disposing);
        }

        #endregion 释放资源
    }
}